package org.jeecg.modules.pucheng.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.pucheng.bo.WgEventDetailBo;
import org.jeecg.modules.pucheng.dto.WgEventDto;
import org.jeecg.modules.pucheng.entity.WgEvent;
import org.jeecg.modules.pucheng.entity.WgEventFile;
import org.jeecg.modules.pucheng.service.IWgEventFileService;
import org.jeecg.modules.pucheng.service.IWgEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 网格事件表
 * @Author: jeecg-boot
 * @Date: 2019-11-17
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "网格事件表")
@RestController
@RequestMapping("/pucheng/wgEvent")
public class WgEventController extends JeecgController<WgEvent, IWgEventService> {
    @Autowired
    private IWgEventService wgEventService;
    @Autowired
    private IWgEventFileService wgEventFileService;

    /**
     * 分页列表查询
     *
     * @param wgEvent
     * @param req
     * @return
     */
    @AutoLog(value = "网格事件表-分页列表查询")
    @ApiOperation(value = "网格事件表-分页列表查询", notes = "网格事件表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(WgEventDto wgEvent,
                                   HttpServletRequest req) {
        WgEvent event = new WgEvent();
        BeanUtil.copyProperties(wgEvent, event);
        QueryWrapper<WgEvent> queryWrapper = QueryGenerator.initQueryWrapper(event, req.getParameterMap());
        int pageNo = wgEvent.getPageNo() == null ? 1 : wgEvent.getPageNo();
        int pageSize = wgEvent.getPageSize() == null ? 10 : wgEvent.getPageSize();
        Page<WgEvent> page = new Page<WgEvent>(pageNo, pageSize);
        IPage<WgEvent> pageList = wgEventService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "网格事件表-按事件查所有")
    @ApiOperation(value = "网格事件表-按事件查所有", notes = "网格事件表-按事件查所有")
    @GetMapping(value = "/queryList")
    public List<WgEvent> queryList(@ApiParam(required = true, name = "eventType", value = "todo 待办事件 done 经办事件 history 归档事件 all 辖区事件 my 我发起的") String eventType) {
        eventType = StringUtils.isNotBlank(eventType) ? eventType.trim().toLowerCase() : "todo";
        LambdaQueryWrapper<WgEvent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WgEvent::getEventType, eventType);
        List<WgEvent> list = wgEventService.list(queryWrapper);
        return list;
    }

    /**
     * 添加
     *
     * @param wgEvent
     * @return
     */
    @AutoLog(value = "网格事件表-添加")
    //@ApiOperation(value = "网格事件表-添加", notes = "网格事件表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody WgEvent wgEvent) {
        wgEventService.save(wgEvent);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param wgEvent
     * @return
     */
    @AutoLog(value = "网格事件表-编辑")
    //@ApiOperation(value = "网格事件表-编辑", notes = "网格事件表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody WgEvent wgEvent) {
        wgEventService.updateById(wgEvent);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "网格事件表-通过id删除")
    //@ApiOperation(value = "网格事件表-通过id删除", notes = "网格事件表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        wgEventService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "网格事件表-批量删除")
    //@ApiOperation(value = "网格事件表-批量删除", notes = "网格事件表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.wgEventService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "网格事件表-通过id查询")
    @ApiOperation(value = "网格事件表-通过id查询", notes = "网格事件表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        WgEvent wgEvent = wgEventService.getById(id);
        WgEventDetailBo eventDetailBo = new WgEventDetailBo();
        BeanUtil.copyProperties(wgEvent,eventDetailBo);
        String idsStr = wgEvent.getEventFileIds();
        if (StringUtils.isNotBlank(idsStr)){
            List<String> ids= new ArrayList<>();
            if (idsStr.endsWith(",")){
                idsStr = idsStr.substring(0,idsStr.length()-1);
            }
            if (idsStr.contains(",")){
                ids.addAll(Arrays.asList(idsStr.split(",")));
            } else {
                ids.add(idsStr);
            }
            LambdaQueryWrapper<WgEventFile> qw = new LambdaQueryWrapper<>();
            qw.eq(WgEventFile::getBizId,wgEvent.getEventId());
            qw.in(WgEventFile::getId,ids);
            List<WgEventFile> files = wgEventFileService.list(qw);
            if (files.size()>0){
                eventDetailBo.setEventFiles(files);
            }
        }
        if (StringUtils.isNotBlank(wgEvent.getSource())){
            String[] names = {"目击", "举报", "微博", "QQ", "留言板", "短信", "热线", "应急指挥中心", "微信", "上报", "网站群", "来访", "来信"};
            String[] types = {"01", "02", "03", "04", "05", "06", "07", "10", "09", "11", "12", "21", "22"};
            for (int i=0;i<types.length;i++){
                if (wgEvent.getSource().trim().equals(types[i])){
                    eventDetailBo.setSource(names[i]);
                }
            }
        }
        if (StringUtils.isNotBlank(wgEvent.getInvolvedNum())){
            String[] names = {"无", "1 人", "2 人", "3-4 人", "5-10 人", "10-30 人"};
            String[] types = {"00", "01", "02", "03", "04", "05"};
            for (int i=0;i<types.length;i++){
                if (wgEvent.getInvolvedNum().trim().equals(types[i])){
                    eventDetailBo.setInvolvedNum(names[i]);
                }
            }
        }
        if (StringUtils.isNotBlank(wgEvent.getCollectWay())){
            String[] names = {"手机", "PC", "呼叫中心", "网站群", "微信"};
            String[] types = {"01", "02", "03", "04", "05"};
            for (int i=0;i<types.length;i++){
                if (wgEvent.getCollectWay().trim().equals(types[i])){
                    eventDetailBo.setCollectWay(names[i]);
                }
            }
        }
        String currentRing = "评价 | 浦城县";
        eventDetailBo.setCurrentRing(currentRing);
        return Result.ok(eventDetailBo);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param wgEvent
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, WgEvent wgEvent) {
        return super.exportXls(request, wgEvent, WgEvent.class, "网格事件表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, WgEvent.class);
    }

    @ApiOperation("事件统计分析_>按部门统计")
    @RequestMapping(value = "/getDataForOrgCount", method = RequestMethod.GET)
    public String getDataForOrgCount() {
        return "{\"names\":[\"县直单位处理\",\"街道（镇）处理\",\"社区（村）自办\"],\"values\":[\"0\",\"0\",\"668\"]}";
    }

    @ApiOperation("各类事件受理情况")
    @RequestMapping(value = "/listDataA", method = RequestMethod.GET)
    public String listDataA() {
        return "{\"overview\":{\"SATISFY\":13,\"SYWBJ\":714,\"NO_SATISFY\":0,\"CJ\":669,\"BJ\":668,\"GQ\":1,\"TOTAL_SATISFY\":13,\"DUBAN\":0,\"TYPE_SATISFY\":\"00\",\"WBJ\":1,\"VERY_SATISFY\":0},\"details\":[{\"CJ\":9,\"BJ\":9,\"TYPE_\":\"08\"},{\"CJ\":9,\"BJ\":9,\"TYPE_\":\"26\"},{\"CJ\":1,\"BJ\":1,\"TYPE_\":\"07\"},{\"CJ\":650,\"BJ\":649,\"TYPE_\":\"31\"}],\"mylDetails\":[{\"SATISFY\":1,\"NO_SATISFY\":0,\"TOTAL_SATISFY\":1,\"TYPE_SATISFY\":\"07\",\"VERY_SATISFY\":0},{\"SATISFY\":12,\"NO_SATISFY\":0,\"TOTAL_SATISFY\":12,\"TYPE_SATISFY\":\"31\",\"VERY_SATISFY\":0}]}";
    }

    @ApiOperation("事件统计分析_>按数量统计")
    @RequestMapping(value = "/getEventNum", method = RequestMethod.GET)
    public String getEventNum() {
        return "{\"status\":0,\"message\":null,\"data\":{\"total\":669,\"codeArray\":[\"350707001\",\"350707002\",\"350707003\",\"350707004\",\"350707005\",\"350707006\",\"350707007\",\"350707008\",\"350707009\",\"350707010\",\"350707011\",\"350707012\",\"350707013\",\"350707014\",\"350707015\",\"350707016\",\"350707017\",\"350707018\",\"350707019\",\"350707020\"],\"valueArray\":[0,0,9,0,0,0,2,0,628,2,0,24,0,0,0,0,0,2,0,2],\"gridIdArray\":[\"212000\",\"211946\",\"246776\",\"218176\",\"240019\",\"240027\",\"241747\",\"244182\",\"244183\",\"244181\",\"245435\",\"245446\",\"245481\",\"245519\",\"245530\",\"245560\",\"245570\",\"246147\",\"246267\",\"246293\"],\"nameArray\":[\"河滨街道\",\"南浦街道\",\"九牧镇\",\"仙阳镇\",\"万安乡\",\"水北镇\",\"石陂镇\",\"濠村乡\",\"官路乡\",\"山下乡\",\"枫溪乡\",\"水北街镇\",\"盘亭乡\",\"管厝乡\",\"永兴镇\",\"古楼乡\",\"莲塘镇\",\"富岭镇\",\"临江镇\",\"忠信镇\"]}}";
    }

    @ApiOperation("事件统计分析_>按类型统计")
    @RequestMapping(value = "/getEventType", method = RequestMethod.GET)
    public String getEventType() {
        return "{\"status\":0,\"message\":null,\"data\":{\"codeArray\":[\"18\",\"03\",\"04\",\"05\",\"06\",\"07\",\"08\",\"09\",\"10\",\"11\",\"12\",\"13\",\"14\",\"16\",\"26\",\"40\",\"01\",\"02\",\"31\",\"68\",\"32\",\"33\",\"34\",\"35\",\"45\",\"50\",\"60\",\"99\"],\"valueArray\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],\"nameArray\":[\"随手拍事件\",\"计生\",\"劳保\",\"民政\",\"安全工作\",\"城市管理\",\"农林水\",\"文明创建\",\"宣传教育\",\"人民武装\",\"经济服务\",\"便民服务\",\"群团工作\",\"车辆违章\",\"施工环境保障\",\"旅游服务\",\"党建\",\"综治维稳\",\"环保\",\"扫黑除恶专项斗争\",\"市场监管\",\"涉茶事件\",\"道安事件\",\"铁路安全\",\"食药监\",\"矛盾纠纷\",\"城市管理（数字城管）\",\"其他\"]}}";
    }


    @ApiOperation("完成统计")
    @RequestMapping(value = "/getComplete", method = RequestMethod.GET)
    public String getComplete() {
        return "{\"status\":0,\"message\":null,\"data\":{\"total\":0,\"complete\":0}}";
    }

}
